$(function () {
    const _token = window.localStorage.getItem('_u_t');
    if (_token == null) {
        console.log('no login')
        $('#login_btn').css('display','block')
        return;
    }
    $.ajax({
        url: 'api/user/main',
        type: 'get',
        headers: {'Authorization':'Bearer ' + _token},
        dataType: 'json',
        timeout: 2000,
        success: function (rel) {
            loadingHide()
            if (rel.code == 0) {
                const mobile =rel.data.mobile;
                const role = rel.data.role;
                $('#nickname').html(mobile+'('+role+')')
                $('#logout_btn').css('display','block')
            } else {
                tispFun(rel.msg);
            }
        },
        error: function () {
            loadingHide()
            tispFun('网络错误,稍后再试');
        },
        complete: function () {
            loadingHide()
        }
    });

    $('#logout_btn').click(function () {
        coco({
            title: "提示",
            text:'确定退出登录吗?'
        }).onClose((cc, isOk, done) => {
            if (cc) {
                loadingShow();
                $.ajax({
                    url: 'api/user/logout',
                    type: 'post',
                    headers: {'Authorization':'Bearer ' + _token},
                    dataType: 'json',
                    timeout: 2000,
                    success: function (rel) {
                        window.localStorage.removeItem('_u_t');
                        if (rel.code == 0) {
                            setTimeout(function (){
                                loadingHide()
                                window.location.href = '/'
                            },1500)
                        } else {
                            tispFun(rel.msg);
                        }
                    },
                    error: function () {
                        loadingHide()
                        tispFun('网络错误,稍后再试');
                        done();
                    },
                    complete: function () {
                        loadingHide();
                        done();
                    }
                });

            } else {
                done();
            }
        });

    })
})
